package com.dongwu.manager.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.dongwu.common.dto.Result;
import com.dongwu.common.utils.UploadFileUtil;

/**
 * 图片上传,音乐上传
 * 
 * @author xyj
 * @date 2017年9月20日
 */
@Controller
@RequestMapping("/file")
public class UploadFileController {
	/**
	 * 上传商品图片
	 * 
	 * @param file
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
	@ResponseBody
	public Result uploadFileImg(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request) {

		if (!file.isEmpty()) {
			// 定义上传图片至项目的文件夹
			String imageSource = "\\images\\";
			// 上传并返回file地址
			String imageUrl = UploadFileUtil.Upload(file, imageSource);
			if (imageUrl != null) {
				// 转换为map以json形式返回给前台文件的路径
				Map<String, String> imgMap= new HashMap<String, String>();
				imgMap.put("image", imageUrl);
				return Result.successResult(imgMap);
			}
		}
		return Result.errorResult("上传图片失败！");

	}


	/**
	 * 上传媒体文件
	 * 
	 * @param file
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadFileAudio", method = RequestMethod.POST)
	@ResponseBody
	public Result uploadFileAudio(@RequestParam(value = "fileToUpload", required = false) MultipartFile file,
			HttpServletRequest request) {
		if (!file.isEmpty()) {
			// 定义上传图片至项目的文件夹
			String AudioSource = "\\Audio\\";
			// 上传并返回file地址
			String AudioUrl = UploadFileUtil.Upload(file, AudioSource);
			if (AudioUrl != null) {
				// 转换为map以json形式返回给前台音乐文件路径
				Map<String, String> AudioMap= new HashMap<String, String>();
				AudioMap.put("Audio", AudioUrl);
				return Result.successResult(AudioMap);
			}
		}
		return Result.errorResult("上传媒体文件！");
	}
}
